﻿@{
    Layout = "";
}
@model Grand.Plugin.DiscountRequirements.Standard.HadSpentAmount.Models.RequirementModel
@using Grand.Framework;

<script>
    $(document).ready(function () {
        $('#savehadspentamountrequirement@(Model.RequirementId)').click(function () {
            var spentAmount = $("#@Html.FieldIdFor(model => model.SpentAmount)").val();
            var discountId = '@Model.DiscountId';
            var requirementId = '@Model.RequirementId';
                
            var postData = {
                discountId: discountId, 
                discountRequirementId: requirementId,
                spentAmount: spentAmount
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "HadSpentAmount"))",
                data: postData,
                success: function (data) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (String.IsNullOrEmpty(Model.RequirementId))
                    {
                        <text>$("#discountRequirementContainer").trigger('grandnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('@Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                }  
            });
        });
    });
</script>
    
<div class="form-horizontal">
        <div class="form-group">
            <admin-label asp-for="SpentAmount" />
            <div class="col-md-9 col-sm-9">
                <div>
                <admin-input asp-for="SpentAmount" />
                <input type="button" id="savehadspentamountrequirement@(Model.RequirementId)" class="btn default" value="@T("Admin.Common.Save")" /></div>
                <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display:none;">@T("Admin.Promotions.Discounts.Requirements.Saved")</div>
            </div>
        </div>
</div>